package com.brsya.aliplayview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;

import com.aliyun.player.AliPlayer;
import com.aliyun.player.AliPlayerFactory;
import com.aliyun.player.IPlayer;
import com.aliyun.player.bean.ErrorInfo;
import com.aliyun.player.bean.InfoBean;
import com.aliyun.player.source.UrlSource;

/**
 * Created by Brsya
 * CreateDate: 2022/5/24  15:04
 * Description:
 * 阿里云播放器View
 */
public class PlayView extends SurfaceView {

    private AliPlayer aliPlayer;

    private PlayListener playListener;

    private String videoUrl;
    private long playTime = -1;

    public PlayView(Context context) {
        super(context);
        init();
    }

    public PlayView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PlayView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        aliPlayer = AliPlayerFactory.createAliPlayer(getContext());
        aliPlayer.setOnPreparedListener(new IPlayer.OnPreparedListener() {
            @Override
            public void onPrepared() {
                if (playListener != null) {
                    playListener.onPrepared();
                }
            }
        });
        aliPlayer.setOnErrorListener(new IPlayer.OnErrorListener() {
            @Override
            public void onError(ErrorInfo errorInfo) {
                if (playListener != null) {
                    playListener.onError(errorInfo);
                }
            }
        });
        aliPlayer.setOnCompletionListener(new IPlayer.OnCompletionListener() {
            @Override
            public void onCompletion() {
                if (playListener != null) {
                    playListener.onCompletion();
                }
            }
        });

        aliPlayer.setOnLoadingStatusListener(new IPlayer.OnLoadingStatusListener() {
            @Override
            public void onLoadingBegin() {
                if (playListener != null) {
                    playListener.onLoadingBegin();
                }
            }

            @Override
            public void onLoadingProgress(int i, float v) {
                if (playListener != null) {
                    playListener.onLoadingProgress(i, v);
                }
            }

            @Override
            public void onLoadingEnd() {
                if (playListener != null) {
                    playListener.onLoadingEnd();
                }
            }
        });

        aliPlayer.setOnStateChangedListener(new IPlayer.OnStateChangedListener() {
            @Override
            public void onStateChanged(int i) {
                 /*
                        int idle = 0;
                        int initalized = 1;
                        int prepared = 2;
                        int started = 3;
                        int paused = 4;
                        int stopped = 5;
                        int completion = 6;
                        int error = 7;
                */
                if (playListener != null) {
                    playListener.onStateChanged(i);
                }
            }
        });

        aliPlayer.setOnInfoListener(new IPlayer.OnInfoListener() {
            @Override
            public void onInfo(InfoBean infoBean) {
                if (playListener != null) {
                    playListener.onInfo(infoBean);
                }
            }
        });

        getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(@NonNull SurfaceHolder holder) {
                aliPlayer.setSurface(holder.getSurface());
            }

            @Override
            public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
                aliPlayer.surfaceChanged();
            }

            @Override
            public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
                aliPlayer.setSurface(null);
            }
        });
    }

    /**
     * 设置播放器id 用于统计数据
     *
     * @param traceId IMEI 或 AndroidId
     */
    public void setTraceId(String traceId) {
        aliPlayer.setTraceId(traceId);
    }

    public String getVideoUrl() {
        return videoUrl;
    }

    public void setPlayTime(long playTime){
        this.playTime = playTime;
    }

    public void setVideoUrl(String videoUrl) {
        this.videoUrl = videoUrl;
        UrlSource urlSource = new UrlSource();
        urlSource.setUri(videoUrl);//播放地址，可以是第三方点播地址，或阿里云点播服务中的播放地址。
        aliPlayer.setDataSource(urlSource);
        aliPlayer.setAutoPlay(true);
        if (playTime > 0) {
            aliPlayer.seekTo(playTime);
        }
        aliPlayer.prepare();
    }

    public void setPlayListener(PlayListener playListener) {
        this.playListener = playListener;
    }

    public AliPlayer getAliPlayer() {
        return aliPlayer;
    }

    public interface PlayListener {
        /**
         * 播放参数
         *
         * @param infoBean 播放参数
         */
        void onInfo(InfoBean infoBean);

        /**
         * 播放状态改变
         *
         * @param status 0:空闲
         *               1:初始化完成
         *               2:准备完成
         *               3:播放中
         *               4:暂停中
         *               5:已停止
         *               6:播放完成
         *               7:播放错误
         */
        void onStateChanged(int status);

        /**
         * 缓存相关 开始缓存
         */
        void onLoadingBegin();

        /**
         * 缓存相关 缓存进度
         *
         * @param progress 缓存进度
         * @param netSpeed 网速
         */
        void onLoadingProgress(int progress, float netSpeed);

        /**
         * 缓存相关 缓存结束
         */
        void onLoadingEnd();

        /**
         * 播放相关 加载完成,可以播放
         */
        void onPrepared();

        /**
         * 播放相关 加载失败
         *
         * @param errorInfo 失败信息
         */
        void onError(ErrorInfo errorInfo);

        /**
         * 播放相关 播放完成
         */
        void onCompletion();
    }

    /**
     * 播放进度改变
     */
    public interface OnProgressChangeListener {
        /**
         * 改变播放进度
         * @param progress 进度
         */
        void changeProgress(long progress);

        /**
         * 播放进度手势处理
         * @param progress 进度
         */
        void changeProgressHint(long progress);
    }
}
